/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * FrmSimulation.java
 *
 * Created on 26-ago-2009, 23:56:08
 */
package gui.forms;

import gui.tools.Receiver;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import lib.exceptions.InvalidLengthException;
import lib.model.Interval;
import lib.simul.Simulation;

/**
 *
 * @author jorge
 */
public class FrmSimulation extends javax.swing.JDialog {

    private Receiver parent;

    /** Creates new form FrmSimulation */
    public FrmSimulation(Receiver parent) {
        initComponents();
        initDates();
        this.parent = parent;

        this.getRootPane().setDefaultButton(btnStart);
    }

    private void initDates() {
        for (int i = 1; i <= 12; i++) {
            cbFromMonth.addItem(i);
            cbToMonth.addItem(i);
        }
        for (int i = 1; i <= 31; i++) {
            cbFromDay.addItem(i);
            cbToDay.addItem(i);
        }
        for (int i = 2005; i <= 2020; i++) {
            cbFromYear.addItem(i);
            cbToYear.addItem(i);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancel = new javax.swing.JButton();
        btnStart = new javax.swing.JButton();
        lblSimulations = new javax.swing.JLabel();
        txtSimulations = new javax.swing.JTextField();
        lblMaxTime = new javax.swing.JLabel();
        txtMaxTime = new javax.swing.JTextField();
        lblSeconds = new javax.swing.JLabel();
        cbFromMonth = new javax.swing.JComboBox();
        cbFromDay = new javax.swing.JComboBox();
        cbToDay = new javax.swing.JComboBox();
        cbToMonth = new javax.swing.JComboBox();
        txtFromHour = new javax.swing.JTextField();
        txtToHour = new javax.swing.JTextField();
        cbFromYear = new javax.swing.JComboBox();
        cbToYear = new javax.swing.JComboBox();
        txtFromMinute = new javax.swing.JTextField();
        lblColonFrom = new javax.swing.JLabel();
        lblColonTo = new javax.swing.JLabel();
        lblFrom = new javax.swing.JLabel();
        lblTo = new javax.swing.JLabel();
        txtToMinute = new javax.swing.JTextField();

        setTitle("New simulation");
        setModal(true);
        setResizable(false);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        lblSimulations.setText("Simulations:");

        lblMaxTime.setText("Maximum time:");

        lblSeconds.setText("seconds");

        cbToDay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbToDayActionPerformed(evt);
            }
        });

        txtFromHour.setText("00");

        txtToHour.setText("00");

        cbToYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbToYearActionPerformed(evt);
            }
        });

        txtFromMinute.setText("00");

        lblColonFrom.setText(":");

        lblColonTo.setText(":");

        lblFrom.setText("From:");

        lblTo.setText("To:");

        txtToMinute.setText("00");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMaxTime)
                            .addComponent(lblSimulations))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSimulations)
                            .addComponent(txtMaxTime, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblSeconds)
                        .addContainerGap(122, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnStart)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancel)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblFrom)
                            .addComponent(lblTo))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbFromMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbFromDay, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbFromYear, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbToMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbToDay, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbToYear, 0, 61, Short.MAX_VALUE)))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(txtToHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblColonTo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(txtFromHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblColonFrom)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtToMinute)
                            .addComponent(txtFromMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSimulations)
                    .addComponent(txtSimulations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMaxTime)
                    .addComponent(txtMaxTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSeconds))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbFromMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbFromDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbFromYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtFromHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblColonFrom)
                    .addComponent(txtFromMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblFrom))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbToMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbToDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbToYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtToHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblColonTo)
                    .addComponent(txtToMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnStart))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-339)/2, (screenSize.height-190)/2, 339, 190);
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        int simuls;
        long mTime;
        try {

            if (txtSimulations.getText().equals("")) {
                txtSimulations.setText("1");
            }
            if (txtMaxTime.getText().equals("")) {
                txtMaxTime.setText("1");
            }

            DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy-HH:mm");
            Date date;
            String string;

            //Start date
            string = cbFromMonth.getSelectedItem().toString() + "/" +
                    cbFromDay.getSelectedItem().toString() + "/" +
                    cbFromYear.getSelectedItem().toString() + "-" +
                    txtFromHour.getText() + ":" +
                    txtFromMinute.getText();
            date = (Date) formatter.parse(string);
            Calendar from = Calendar.getInstance();
            from.setTime(date);

            //End date
            string = cbToMonth.getSelectedItem().toString() + "/" +
                    cbToDay.getSelectedItem().toString() + "/" +
                    cbToYear.getSelectedItem().toString() + "-" +
                    txtToHour.getText() + ":" +
                    txtToMinute.getText();
            date = (Date) formatter.parse(string);
            Calendar to = Calendar.getInstance();
            to.setTime(date);

            Interval interval = new Interval(from, to);
            simuls = Integer.parseInt(txtSimulations.getText());
            mTime = Long.parseLong(txtMaxTime.getText()) * 1000;
            parent.reciveObject(new Simulation(simuls, mTime, interval));
            this.setVisible(false);
        } catch (InvalidLengthException ile) {
            JOptionPane.showMessageDialog(rootPane,
                    ile.getMessage(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n The number of simulations and the maximum time\n" +
                    "must be bigger than zero.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnStartActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void cbToDayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbToDayActionPerformed
}//GEN-LAST:event_cbToDayActionPerformed

    private void cbToYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbToYearActionPerformed
}//GEN-LAST:event_cbToYearActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnStart;
    private javax.swing.JComboBox cbFromDay;
    private javax.swing.JComboBox cbFromMonth;
    private javax.swing.JComboBox cbFromYear;
    private javax.swing.JComboBox cbToDay;
    private javax.swing.JComboBox cbToMonth;
    private javax.swing.JComboBox cbToYear;
    private javax.swing.JLabel lblColonFrom;
    private javax.swing.JLabel lblColonTo;
    private javax.swing.JLabel lblFrom;
    private javax.swing.JLabel lblMaxTime;
    private javax.swing.JLabel lblSeconds;
    private javax.swing.JLabel lblSimulations;
    private javax.swing.JLabel lblTo;
    private javax.swing.JTextField txtFromHour;
    private javax.swing.JTextField txtFromMinute;
    private javax.swing.JTextField txtMaxTime;
    private javax.swing.JTextField txtSimulations;
    private javax.swing.JTextField txtToHour;
    private javax.swing.JTextField txtToMinute;
    // End of variables declaration//GEN-END:variables
}
